<?php
class AdminController extends Zend_Controller_Action
{
	protected $db; // 数据库指针

	// 初始化
	function init()
	{
		// 取得数据库指针
		$this->db=Zend_Registry::get('database');
		$this->view->action=$this->_getParam('action');
		$this->view->controller=$this->_getParam('controller');

		// 页面css和js
		$this->view->javascript=array('paper.js');
	}

	// 处理no action异常
	function __call($method, $args)
	{
		if ('Action'==substr($method, -6)) {
			$this->view->action=substr($method, 0, -6);
			$this->view->param=$this->_getAllParams();
			$this->renderScript('error/no-action.php');
		}
		else // 其他异常
			$this->_redirect('/');
	}

	/* 下面是action处理函数: ***************************/

	// 授权或解除权窗口
	function assignAction()
	{
		if (!auth::check(auth::PRIV_ADMIN)) {
			$this->renderScript('error/no-action.php');
			return;
		}

		$db = new AccountDb();
		$this->view->teacher = $db->Teacher(); 
		$this->view->phd = $db->Phd();
		$this->view->master = $db->Master();
		$this->view->graduate = $db->Graduate();
	}

	// 更新网站管理人列表
	function updateAction()
	{
		if (!auth::check(auth::PRIV_ADMIN)) {
			$this->view->msg = "你的权限不够。";
			$this->renderScript('error/small-msg.php');
			return;
		}

		$str = $this->_getParam('member');
		$admin = paper::str_to_author($str);
		$fp = fopen($_SERVER['DOCUMENT_ROOT'].
					'/app/views/scripts/static/admin.php', "w");
		fwrite($fp, '<?php /* 管理员列表，不要手工修改 */');
		foreach ($admin as $id => $name) {
			$name = stripslashes($name);
			fwrite($fp, "\n\$admin[$id]='$name';");
		}
		fwrite($fp, "\n?>");
		fclose($fp);

		echo $str;
		$this->_helper->viewRenderer->setNoRender();
	}

	// 更改内容
	function changeAction()
	{
		// 管理权限判断
		// 从static/admin.php中读出$admin变量，下标为管理员id
		$admin = array();
		@ include 'static/admin.php';
		$aid = auth::aid();
		if (!array_key_exists($aid, $admin)) {
			$this->view->msg = "你的权限不够。";
			$this->renderScript('error/small-msg.php');
			return;
		}

		$name = $this->_getParam('name');
		if (!preg_match("/\b$name\b/", 
					'home|intro|interest|compiler|programming')) {
			$this->view->msg = "出错。";
			$this->renderScript('error/small-msg.php');
			return;
		}
		$pathname = $_SERVER['DOCUMENT_ROOT'].
				'/app/views/scripts/static/'.$name.'.php';
		$fp = fopen($pathname, "w");
		$cont = $this->_getParam('content');
		$cont = Admin::Strip($cont);
		fwrite($fp, $cont);
		fclose($fp);

		$this->view->msg = '操作完成。';
		$this->renderScript('error/small-msg.php');
	}

	// 首页管理
	function indexAction()
	{
		$this->view->title='首页管理－软件教研室后台';
	
		// 管理权限判断
		// 从static/admin.php中读出$admin变量，下标为管理员id
		$admin = array();
		include 'static/admin.php';
		$aid = auth::aid();
		if (!array_key_exists($aid, $admin)) {
			$this->view->msg = "你的权限不够。";
			$this->renderScript('error/small-msg.php');
			return;
		}

		$this->view->name = $this->_getParam('name');
	}

	// 介绍页管理
	function introAction()
	{
		$this->view->title='介绍页管理－软件教研室后台';
		// 管理权限判断
		// 从static/admin.php中读出$admin变量，下标为管理员id
		$admin = array();
		include 'static/admin.php';
		$aid = auth::aid();
		if (!array_key_exists($aid, $admin)) {
			$this->view->msg = "你的权限不够。";
			$this->renderScript('error/small-msg.php');
			return;
		}

	}

	// 研究方向及成果
	function interestAction()
	{
		$this->view->title='研究方向及成果页管理－软件教研室后台';
		// 管理权限判断
		// 从static/admin.php中读出$admin变量，下标为管理员id
		$admin = array();
		include 'static/admin.php';
		$aid = auth::aid();
		if (!array_key_exists($aid, $admin)) {
			$this->view->msg = "你的权限不够。";
			$this->renderScript('error/small-msg.php');
			return;
		}

	}

	// 研究成果
	function fruitAction()
	{
		$this->view->title='研究成果－软件教研室';
		// 管理权限判断
		// 从static/admin.php中读出$admin变量，下标为管理员id
		$admin = array();
		include 'static/admin.php';
		$aid = auth::aid();
		if (!array_key_exists($aid, $admin)) {
			$this->view->msg = "你的权限不够。";
			$this->renderScript('error/small-msg.php');
			return;
		}

	}

};
?>
